Ext.require([
	 'Ext.grid.*',
	 'Ext.window.Window',
	 'Ext.container.Viewport',
	 'Ext.layout.container.Border',
	 'Ext.state.*',
	 'Ext.data.*',
	 'Ext.tree.*'
]);

/**
 * Affichage de la liste des mix.
 */
function displayListeMix(data) {
	$("#treeMix").empty();
	$("#treeMix").jstree({
		plugins: ["themes", "json_data", "crrm", "ui", "unique", "types", "search"],
		json_data : { 
			data: data
 		} 
	}).bind("change_state.jstree", function (e, data) {
	}).bind("select_node.jstree", function (e, data) {
		if (data.inst.get_path().length == 3) {
			cs_network_selected = data.rslt.obj.context.id.split('-')[0];
			//cs_division_selected = data.rslt.obj.context.id.split('-')[1];
			cs_adm_pdt_selected = data.rslt.obj.context.id.split('-')[1];
			$('#centerEastDivTitleId').html(data.rslt.obj.context.innerText);
			$(".centerEastDivInfo").css("display", "block");
			$('#adminSalesReport').css("display", "none");
			$('#accessAdminId').css("display", "none");
			$('#accessSalesReportId').css("display", "block");
			$('#salesReport').css("display", "block");
		} else {
			$('#centerEastDivTitleId').html("S&eacute;lectionner un produit, pour cela d&eacute;plier l\'arbre");
			$(".centerEastDivInfo").css("display", "none");
		}
	}).bind("open_node.jstree", function (e, data) {
		setTimeout('resizeEastPart();', 500);
	}).bind("close_node.jstree", function (e, data) {
		setTimeout('resizeEastPart();', 500);
  	}).bind("loaded.jstree", function (event, data) {
  		//$(this).jstree("open_all"); // pour ouvrir tout l arbre 
  		var h = $('#centerWestDivId').css('height');
  		$('#centerEastDivId').css('height', h);
	}); 
}

/**
 * Affichage de la liste des mix (admin).
 */
function displayListeMixAdmin(data) {
	$("#treeMixAdmin").empty();
	$("#treeMixAdmin").jstree({
		plugins: ["themes", "json_data", "crrm", "ui", "unique", "types", "search"],
		json_data : { 
			data: data
 		} 
	}).bind("change_state.jstree", function (e, data) {
	}).bind("select_node.jstree", function (e, data) {
		if (data.inst.get_path().length == 3) {
			cs_network_selected = data.rslt.obj.context.id.split('-')[0];
			//cs_division_selected = data.rslt.obj.context.id.split('-')[1];
			cs_adm_pdt_selected = data.rslt.obj.context.id.split('-')[1];
			$('#centerEastDivTitleId').html(data.rslt.obj.context.innerText);
			$(".centerEastDivInfo").css("display", "block");
			$('#accessAdminId').css("display", "block");
			$('#accessSalesReportId').css("display", "none");
			$('#salesReport').css("display", "none");
		} else {
			$('#centerEastDivTitleId').html("S&eacute;lectionner un produit, pour cela d&eacute;plier l\'arbre");
			$(".centerEastDivInfo").css("display", "none");
		}
	}).bind("open_node.jstree", function (e, data) {
		setTimeout('resizeEastPart();', 500);
	}).bind("close_node.jstree", function (e, data) {
		setTimeout('resizeEastPart();', 500);
  	}).bind("loaded.jstree", function (event, data) {
  		//$(this).jstree("open_all"); // pour ouvrir tout l arbre 
  		var h = $('#centerWestDivId').css('height');
  		$('#centerEastDivId').css('height', h);
	}); 
}


/**
 * Adapte la hauteur de la partie Est identique à la partie Ouest.
 */
function resizeEastPart() {
	var h = $('#centerWestDivId').css('height');
	$('#centerEastDivId').css('height', h);
}

/**
 * Recherche en base de tous les mix.
 */
function listeMix() {
	$.ajax({
 		traditional: true, 
 		type: 'POST', 
 		url: './listeMix.do',
 		target: "_blank", 
 		data : {},
 		error: function () {},
 		success: function(result) { 
 			displayListeMix(result.data);
 			$('#loginUserId').html("<img src='img/GSK.jpg' style='width:20px;'/>&nbsp;&nbsp;<img src='img/user.png' style='width:10px;'/>&nbsp;&nbsp;<u>Utilisateur</u> : <span style='font-style:italic;'>" + result.login + "</span>");
 		}
	});
}

/**
 * Recherche en base de tous les mix (admin).
 */
function listeMixAdmin() {
	$.ajax({
 		traditional: true, 
 		type: 'POST', 
 		url: './listeMixAdmin.do',
 		target: "_blank", 
 		data : {},
 		error: function () {},
 		success: function(result) { 
 			displayListeMixAdmin(result.data);
 		}
	});
}

/**
 * Chargement de la page.
 */
Ext.onReady(function() {
	
	// Contenu de la page d accueil 
	Ext.create('Ext.container.Viewport', {
		layout: 'border',
		items: [
		    {
		    	// Zone NORD
		    	region:			'north',
		    	split:			true, 
				collapsible:	true,
				title:			'<span style="font-size:18px;">Bienvenue</span>',
				bodyStyle:		'background-color: #F5F5F5;',
		    	html: [
		    	       '<div style="padding-top:5px;height:25px;" class="accueilBorder text-general">', 
			    	       '<div style="float:left;width:50%;padding-left:5px;font-size:14px;"	align="left">',
			    	       		'&bull; Bienvenue sur l\'application <b style="color:#04408c;">Sales Report</b>',
			    	       '</div>',
			    	       '<div style="float:right;padding-right:10px;" align="right" id="loginUserId"></div>',
		    	       '</div>'
		    	]
		    }, 
	        {
		    	// Zone CENTRE 
	        	region:		'center',
	        	height:		'90%',
	        	title:		'<span style="font-size:18px;">Mix</span>',
	        	bodyStyle: 'background-color: #F5F5F5;',
	        	autoScroll: true,
	        	html: [
	        	       	// Formulaire admin
						'<form method="post" target="" id="formAdmin" style="display:none" action="admin.do" >',
							'<input type="hidden" name="cs_adm_pdt" id="cs_adm_pdt_form" />',
							'<input type="hidden" name="cs_network" id="cs_network_form" />',
							//'<input type="hidden" name="cs_division" id="cs_division_form" />',
						'</form>',
						// Formulaire sales report
						'<form method="post" target="" id="formSalesReport" style="display:none" action="default.do" >',
							'<input type="hidden" name="cs_adm_pdt" id="cs_adm_pdt_form_sr" />',
							'<input type="hidden" name="cs_network" id="cs_network_form_sr" />',
							//'<input type="hidden" name="cs_division" id="cs_division_form_sr" />',
						'</form>',
	        	       // Contenu
	        	       '<div id="centerWestDivId" class="accueilBorder text-general" style="float:left;width:49%;">', 
		        	       '<div style="padding-top:5px;" class="adminTitle">&bull; S&eacute;lectionner un produit dans l\'arbre suivant :</div>',
		        	       '<div style="padding-top:5px;padding-left:20px;" id="treeMix"></div>',
		        	       '<div style="padding-top:5px;" class="adminTitle" id="treeAdminId">&bull; S&eacute;lectionner un produit dans l\'arbre suivant : (Administration)</div>',
		        	       '<div style="padding-top:5px;padding-left:20px;" id="treeMixAdmin"></div>',
		        	       '<div style="padding-top:10px;" id="createProductId" class="adminTitle">&bull; Cr&eacute;ation d\'un produit :</div>',
		        	       '<div style="padding-top:5px;padding-bottom:5px;" id="goAdmin"></div>',
	        	       '</div>',
	        	       '<div id="centerEastDivId" class="accueilBorder text-general" style="float:right;width:49%;">',
	        	       		'<div id="centerEastDivTitleId" style="padding-top:5px;font-weight:bold;color:#04408c;"></div>',
	        	       		'<div class="centerEastDivInfo" style="padding-top:5px;display:none;" id="accessSalesReportId">&bull; Acc&eacute;dez au SalesReport :</div>',
	        	       		'<div class="centerEastDivInfo" style="padding-top:5px;padding-bottom:5px;display:none;" id="salesReport"></div>',
	        	       		'<div class="centerEastDivInfo" style="padding-top:5px;display:none;" id="accessAdminId">&bull; Acc&eacute;dez &agrave; l\'admin :</div>',
	        	       		'<div class="centerEastDivInfo" style="padding-top:5px;padding-bottom:5px;display:none;" id="adminSalesReport"></div>',
	        	       '</div>', 
	        	       '<div id="footerAccueil" class="accueilBorder text-general" style="float:left;width:99%;">&copy; Version Beta 1.0.1</div>'
	        	]
	        }
		]
	});
	
	// Bouton pour se rendre sur l interface d administration en mode création 
	if (isAdminSR) {
		Ext.create('Ext.Button', {
		    text: 'Cr&eacute;er',
		    renderTo: 'goAdmin',
		    handler: function() {}
		});
		var goAdmin = Ext.get('goAdmin'); 
		goAdmin.on('click', function() {
			$('#cs_adm_pdt_form').val(0);
			$('#cs_network_form').val(0);
			//$('#cs_division_form').val(0);
			document.getElementById('formAdmin').submit();
	    });
	} else {
		$('#createProductId').css("display", "none");
	}
	
	// Bouton pour se rendre sur l interface SalesReport 
	Ext.create('Ext.Button', {
	    text: 'SalesReport',
	    renderTo: 'salesReport',
	    handler: function() {}
	});
	var salesReport = Ext.get('salesReport'); 
	salesReport.on('click', function() {
		$('#cs_adm_pdt_form_sr').val(cs_adm_pdt_selected);
		$('#cs_network_form_sr').val(cs_network_selected);
		//$('#cs_division_form_sr').val(cs_division_selected);
		document.getElementById('formSalesReport').submit();
    });
	
	// Bouton pour se rendre sur l administration du produit  
	if (isAdminSR) {
		Ext.create('Ext.Button', {
		    text: 'Modifier',
		    renderTo: 'adminSalesReport',
		    handler: function() {}
		});
		var adminSalesReport = Ext.get('adminSalesReport'); 
		adminSalesReport.on('click', function(){
			$('#cs_adm_pdt_form').val(cs_adm_pdt_selected);
			$('#cs_network_form').val(cs_network_selected);
			//$('#cs_division_form').val(cs_division_selected);
			document.getElementById('formAdmin').submit();
	    });
	}
	
	// Recherche tous les mix 
	listeMix();
	
	// Arbre affiché pour l'administrateur  
	if (isAdminSR) {
		listeMixAdmin();
	} else {
		$('#treeAdminId').css("display", "none");
	}
	
});